/****************************************************
*	OSMF Edge Media Player version 1.2.0
* 
*	Author: Emanuele Manco
* 	E-Mail: hello@flashedge.net
*	Web: http://activeden.net/user/flashedge
*  
*****************************************************/

package net.flashedge.display 
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.text.AntiAliasType;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import com.greensock.*;
	import com.greensock.easing.*;
	
	public class SettingsPanel extends Sprite
	{
		 
		public var settings:Settings = new Settings();
		
		public var shuffleEnabled:Boolean = false;
		public var loopEnabled:Boolean = false;
		
		public var bg:Sprite;
		public var setup;
		public var player;
		
		public var status:Sprite = new Sprite();
		public var statusField:TextField;
		
		public var enabled:Boolean = true;
		public var primary:primaryFont = new primaryFont();
		
		public var scaleHidden:Boolean = false;
		
		public function SettingsPanel() 
		{
			
		}
		
		public function init():void {
			// background
			bg = new Sprite();
			bg.name = "bg";
				
			bg.graphics.lineStyle(1, 0xE2E2E2, 0.2, true);
			bg.graphics.beginFill(0x000000, 0.6);
			bg.graphics.drawRoundRect(0, 0, 192, 60, 10, 10);
			bg.graphics.endFill();
				
			// filters
			var bgFilters = new GlowFilter(0x000000, 0.2, 8, 8, 2, 1, false);
			bg.filters = [bgFilters]; 
				
			addChild(bg);
			addChild(settings);
			settings.x = 16;
			settings.y = 10;
			
			addChild(status);
			status.y = 64;
			status.x = 0;
			
			configure();
			
			if (setup.shuffleEnabled == "true") {
				shuffleEnabled = true;
				TweenMax.to(settings.shuffle, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0.5, blurX:8, blurY:8 } } );
			}
				
			if (setup.loopEnabled == "true") {
				loopEnabled = true;
				TweenMax.to(settings.loop, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0.5, blurX:8, blurY:8 } } );
			}
		}
		
		public function configure():void {
			for (var i = 0; i < settings.numChildren; i++ ) {
					
					var obj = settings.getChildAt(i);
					obj.buttonMode = true;
					
					obj.addEventListener(MouseEvent.MOUSE_OVER, btnOver);
					obj.addEventListener(MouseEvent.MOUSE_OUT, btnOut);
					
					if (obj.name != "scale") {	// the scale button is handled in the main class
						obj.addEventListener(MouseEvent.CLICK, btnClick);
					}
			}
			
				// the description text field
				statusField = new TextField();
				
				statusField.name = "field";
				statusField.autoSize = TextFieldAutoSize.LEFT;
				statusField.width = 190;
				statusField.background = false;
				statusField.embedFonts = true;
				statusField.border = false;
				statusField.selectable = false;
				statusField.antiAliasType = AntiAliasType.ADVANCED;
				statusField.wordWrap = true;
				
				var statusFormat:TextFormat = new TextFormat();
				
				statusFormat.font = primary.fontName; 
				statusFormat.size = 13;
				statusFormat.letterSpacing = 0;
				statusField.condenseWhite = true;
				statusFormat.color = 0xFFFFFF;
				statusFormat.rightMargin = 20;
				statusFormat.leftMargin = 8;
				
				statusField.defaultTextFormat = statusFormat;
				
				statusField.text = "";
				statusField.y = 6;
				
				refreshBg();
				
				status.addChild(statusField);
				
				status.alpha = 0;
		}
		
		public function refreshBg():void
		{
			status.graphics.clear();
			status.graphics.lineStyle(1, 0xE2E2E2, 0.2, true);
			status.graphics.beginFill(0x000000, 0.6);
			status.graphics.drawRoundRect( 0, 0, statusField.textWidth+20, 30, 10, 10);
			status.graphics.endFill();
		}
		
		private function btnClick(e:MouseEvent):void 
		{
			switch (e.currentTarget.name) {
			case "shuffle": 
			if (shuffleEnabled) {
				TweenMax.to(e.currentTarget, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0, blurX:0, blurY:0 } } );
				shuffleEnabled = false;
				statusField.text = setup.shuffleDisabledLabel;
				} else {
				TweenMax.to(e.currentTarget, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0.5, blurX:8, blurY:8 } } );
				shuffleEnabled = true;
				statusField.text = setup.shuffleEnabledLabel;
				}
			break;
			case "loop":
			if (loopEnabled) {
				TweenMax.to(e.currentTarget, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0, blurX:0, blurY:0 } } );
				loopEnabled = false;
				statusField.text = setup.loopDisabledLabel;
				} else {
				TweenMax.to(e.currentTarget, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0.5, blurX:8, blurY:8 } } );
				loopEnabled = true;
				statusField.text = setup.loopEnabledLabel;
				}
			break;
			}
			trace ("clicked: " + e.currentTarget.name);
			
			refreshBg();
			if (scaleHidden) { 
				status.x = Math.floor( (135 - statusField.textWidth - 20) * .5 ) 
				} else {
				status.x = Math.floor( (192 - statusField.textWidth - 20) * .5 ) 
				}
			TweenMax.to(status, 0.5, { autoAlpha:1 } );
		}
	
		
		private function btnOut(e:Event):void 
		{
			switch (e.currentTarget.name) {
			case "shuffle": if (shuffleEnabled == false) { TweenMax.to(e.currentTarget, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0, blurX:0, blurY:0 } } ); } 
			break;
			case "loop": if (loopEnabled == false ) { TweenMax.to(e.currentTarget, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0, blurX:0, blurY:0 } } ) };
			break;
			case "scale": TweenMax.to(e.currentTarget, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0, blurX:0, blurY:0 } } )
			break;
			}				
		}
		
		private function btnOver(e:Event):void 
		{
			TweenMax.to(e.currentTarget, 0.5, { glowFilter: { color:0xFFFFFF, alpha:0.5, blurX:8, blurY:8 } } );
		}
		
		public function arrange(w:Number, h:Number):void {
			
			if (scaleHidden) {
				this.x = Math.floor( (w - 135) * .5 );
				this.y =  Math.floor(  (h - this.height) * .5);
				
				status.x = Math.floor( (135 - statusField.textWidth - 20) * .5 );
			} else {
				this.x = Math.floor( (w - 192) * .5 );
				this.y =  Math.floor(  (h - this.height) * .5);
				
				status.x = Math.floor( (192 - statusField.textWidth - 20) * .5 );
				}
			
		}
		
		public function hideScale():void {
			settings.scale.visible = false;
			
			bg.graphics.clear();
			bg.graphics.lineStyle(1, 0xE2E2E2, 0.2, true);
			bg.graphics.beginFill(0x000000, 0.6);
			bg.graphics.drawRoundRect(0, 0, 135, 60, 10, 10);
			bg.graphics.endFill();
			
			scaleHidden = true;
		}
		
		public function showScale():void {
			settings.scale.visible = true;
			
			bg.graphics.clear();
			bg.graphics.lineStyle(1, 0xE2E2E2, 0.2, true);
			bg.graphics.beginFill(0x000000, 0.6);
			bg.graphics.drawRoundRect(0, 0, 192, 60, 10, 10);
			bg.graphics.endFill();
			
			scaleHidden = false;
		}
		
		public function clear():void {
			
			for (var i = 0; i < settings.numChildren; i++ ) {
					
					var obj = settings.getChildAt(i);
					
					obj.removeEventListener(MouseEvent.MOUSE_OVER, btnOver);
					obj.removeEventListener(MouseEvent.MOUSE_OUT, btnOut);
					
					if (obj.name != "scale") {	// the scale button is handled in the main class
						obj.removeEventListener(MouseEvent.CLICK, btnClick);
					}
			}
					
			status.graphics.clear();
			bg.graphics.clear();
			
			removeChild(bg);
			removeChild(settings);
			
			removeChild(status);
			
			

		}
	}

}